<!-- 微信登录		login -->
<template>
	<view class="">
		<view class="content">
			<!-- 企业logo图标 -->
			<view class="logo senter">
				<image src="../../static/logo.jpg" style="height: 280rpx;width: 280rpx;" mode=""></image>
			</view>
			
			<view class="text-area">
				<button class="but center" open-type="getPhoneNumber" @getphonenumber="getphonenumber">微信用户一键登录</button>
			</view>
			<view class="text-area mt10 " @click="gophonelogin">
				<view  class="but bg-f5f c-999 center" style="border: none;">其它手机号登录</view>
			</view>
		<!-- 	<view class="senter f d c fs16 p_t_31">
				<text>暂不登录</text>
				<text class="p_t_25">温馨提示：登录后才能正常使用设备，暂不登录将无法正常使用。</text>
			</view> -->
		</view>
		
		<u-popup v-model="show" mode="center" width="80%" border-radius="20">
			<view class="center" >
				<view class="bb" style="height: 100rpx;line-height: 100rpx;">
					选择用户
				</view>
				<view class=" pb20 dis-flex flex-wrap">
					<view @click="gouser(item)" class="wd50 mt20" v-for="(item,index) in userList">
						<image :src="item.img" mode="" style="border-radius: 200rpx;width: 130rpx;height: 130rpx;"></image>
						<view class="c-999 fs26 mt5 ellipsis pl20 pr20">
							{{item.name}}
						</view>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginResCode:'',
				show:false,
				userList:[]
			}
		},
		onLoad() {
			// 动态导航条文字
			uni.setNavigationBarTitle({
				title: '登录'
			})
			var that = this
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					
					that.loginResCode = loginRes.code
				}
			});
		},
		methods: {
			gophonelogin(){
				this.$api.openWindow('/pages/login/phone_login')
			},
			getphonenumber(e){
				var that = this
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					console.log('用户拒绝提供手机号');
				} else {
					console.log('用户同意提供手机号');
					console.log("iv", e.detail.iv)
					console.log("encryptedData", e.detail.encryptedData)
					var encryptedData = e.detail.encryptedData
					var iv = e.detail.iv
					//检查登录
					uni.checkSession({
						success: (res) => {
							console.log("checkSession--success---res", res)
							if (res.errMsg == 'checkSession:ok') {
								console.log(res);
								console.log('登录暂未过期');
								if (encryptedData !== undefined && iv !== undefined) {
									that.getPhone(that.loginResCode, encryptedData, iv)
								}
							}
						},
						fail: (res) => {
							console.log('登录已过期', res);
							//再执行一次login
							uni.login({
								provider: 'weixin',
								success: function(loginRes) {
									that.loginResCode = loginRes.code
									if (encryptedData !== undefined && iv !== undefined) {
										that.getPhone(that.loginResCode, encryptedData, iv)
									}
								}
							});
						}
					})
				}
			},
			//登录
			getPhone(loginResCode, encryptData, ivStr) {
				var that = this
				var json={
					code:loginResCode,
					encryptedData:encryptData,
					iv:ivStr,
					upid:uni.getStorageSync('scene')
				}
				that.$http.post(that.$config.UrlPostlogin, json).then((res) => {
					if (res.status == 1 && res.userList.length > 1) {
						this.userList = res.userList
						this.show = true
					
					}else{
						uni.setStorageSync('token', res.data);
						uni.removeStorageSync('scene');//登录成功后删除二维码里的参数
						that.$api.toast('登录成功')
						setTimeout(()=>{
							that.$api.openWindow('/pages/mine/mine','switchTab')
						},500)
					}
				})
			},
			//选择用户
			gouser(item){
				var that = this
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						var json={
							code: loginRes.code,
							token:item.token
						}
						that.$http.post(that.$config.UrlPostselectUser, json).then((res) => {
							if (res.status == 1 ) {
								uni.setStorageSync('token', item.token);
								that.$api.toast('登录成功')
								setTimeout(()=>{
									that.$api.openWindow('/pages/mine/mine','switchTab')
								},500)
							}
						})
					}
				});
				
				
			}
		}
	}
</script>

<style>
	page{
		background-color: #FFFFFF;
	}
	.warps {
		overflow: hidden;
		background-color: #FFFFFF;
	}
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		width: 280rpx;
		height: 280rpx;
		line-height:280rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
		background: #ececec;
		overflow:hidden;
	}

	.text-area {
		display: flex;
		justify-content: center;
		margin-top:80rpx;
	}
	.but{
		width: 600rpx;
		height: 76rpx;
		line-height:76rpx;
		opacity: 1;
		background: #1f73f2;
		border-radius: 16rpx;
		font-size:28rpx;
		color:#fff;
	}
	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
